package com.lwj.project1;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;
import androidx.viewpager2.widget.ViewPager2;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;

import com.google.android.material.tabs.TabLayout;
import com.lwj.project1.RecommendPageFragments.AmericanFragment;
import com.lwj.project1.RecommendPageFragments.GermanyFragment;
import com.lwj.project1.RecommendPageFragments.PartyFragment;
import com.lwj.project1.RecommendPageFragments.TRPGFragment;
import com.lwj.project1.RecommendPageFragments.TraditionFragment;
import com.lwj.project1.RecommendPageFragments.WarFragment;

import java.util.ArrayList;

public class RecommendFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        return inflater.inflate(R.layout.fragment_recommend, container, false);
    }
    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle saveInstanceState) {
        super.onViewCreated(view,saveInstanceState);
        ArrayList<Fragment> fragments = new ArrayList<Fragment>();
        fragments = initData(fragments);
        ViewPager viewPager = view.findViewById(R.id.page_view);
        MyPagerAdapter myPagerAdapter = new MyPagerAdapter(getChildFragmentManager(), fragments);
        viewPager.setAdapter(myPagerAdapter);

        TabLayout tabLayout = view.findViewById(R.id.tabLayout);
        tabLayout.setOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(viewPager));
        viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
    }

    private ArrayList<Fragment> initData(ArrayList<Fragment> fragments) {
        AmericanFragment americanFragment = new AmericanFragment();
        GermanyFragment germanyFragment = new GermanyFragment();
        TRPGFragment trpgFragment = new TRPGFragment();
        WarFragment warFragment = new WarFragment();
        PartyFragment partyFragment = new PartyFragment();
        TraditionFragment traditionFragment = new TraditionFragment();
        fragments.add(americanFragment);
        fragments.add(germanyFragment);
        fragments.add(trpgFragment);
        fragments.add(warFragment);
        fragments.add(partyFragment);
        fragments.add(traditionFragment);
        return fragments;
    }
}